package com.ihmp.dao;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.ihmp.model.TJobTitle;

@Repository("jobTitleDAO")
public class CJobTitleDAO implements IJobTitleDAO {

	@Autowired
	private SessionFactory sessionFactory;
	
	public void saveOrUpdate(TJobTitle jobtitle) {		
		sessionFactory.getCurrentSession().saveOrUpdate(jobtitle);
	}
	
	public void delete(TJobTitle jobtitle) {		
		sessionFactory.getCurrentSession().delete(jobtitle);
	}

	public TJobTitle get(Integer id) {		
		return (TJobTitle) sessionFactory.getCurrentSession().get(TJobTitle.class, id);
		
	}

	@SuppressWarnings("unchecked")
	public List<TJobTitle> list() {		
		Criteria crit = sessionFactory.getCurrentSession().createCriteria(TJobTitle.class);
		crit.addOrder(Order.desc("createDate"));
		crit.addOrder(Order.desc("editDate"));
		return crit.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<TJobTitle> list(int page,int recordPerPage) {		
		Criteria crit = sessionFactory.getCurrentSession().createCriteria(TJobTitle.class);
		crit.setFirstResult((page-1)*recordPerPage).setMaxResults(page*recordPerPage -1);
		crit.addOrder(Order.desc("createDate"));
		crit.addOrder(Order.desc("editDate"));
		return crit.list();
	}
	
	public LinkedHashMap<Integer, String> getComboData(){
		LinkedHashMap<Integer, String> map = new LinkedHashMap<Integer, String>();
		ArrayList<TJobTitle> jobtitleList = (ArrayList<TJobTitle>) list();
		for(TJobTitle jobtitle : jobtitleList){
			map.put(jobtitle.getId(), jobtitle.getName());
		}
		return map;
	}
	
}
